﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FacadeSingTowerMall;
using DTOSingTowerMall;

namespace WebFormSingTowerMall.Approval
{
    public partial class EventHallBookingApproval : System.Web.UI.Page
    {
        ApprovalFacade af = new ApprovalFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EventHallBookingGridView.DataSource = af.getEVHBooking();
                EventHallBookingGridView.DataBind();
                if (EventHallBookingGridView.Rows.Count == 0)
                    Label1.Text = "No any application now";
            }
        }

        protected void lnkViewDetail_click(object _sender, EventArgs _args)
        {
            String username = ((System.Web.UI.WebControls.CommandEventArgs)(_args)).CommandArgument.ToString();
            if (username != string.Empty)
                Response.Redirect("../Approval/ViewUserDetail.aspx?UserName=" + username);
        }

        protected void EventHallBookingGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            int hallid = Convert.ToInt32(EventHallBookingGridView.SelectedRow.Cells[1].Text);
            List<IEventHallBooking> allbooking = af.getApprovedEVHBooking(hallid).ToList();
            List<string> eventhallid = new List<string>();
            List<string> eventstartdate = new List<string>();
            List<string> eventenddate = new List<string>();
            Label1.Text = null;
            if (allbooking.Count != 0)
            {
                //for (int i = 0; i < allbooking.Count; i++)
                foreach (IEventHallBooking booking in allbooking)
                {
                    if (booking.Status == "Approved")
                    {
                        eventhallid.Add(booking.HallID.ToString());
                        eventstartdate.Add(booking.EventStartDateTime.ToString());
                        eventenddate.Add(booking.EventEndDateTime.ToString());
                    //eventhallid = allbooking.Select(a => a.HallID.ToString()).ToList();
                    //eventstartdate = allbooking.Select(a => a.EventStartDateTime.ToString()).ToList();
                    //eventenddate = allbooking.Select(a => a.EventEndDateTime.ToString()).ToList();
                     }
                }
            }
            for (int i = 0; i < EventHallBookingGridView.Rows.Count; i++)
            {
                if (EventHallBookingGridView.Rows[i].Cells[8].Text == "Approved")
                {
                    eventhallid.Add(EventHallBookingGridView.Rows[i].Cells[1].Text);
                    eventstartdate.Add(EventHallBookingGridView.Rows[i].Cells[4].Text);
                    eventenddate.Add(EventHallBookingGridView.Rows[i].Cells[5].Text);
                }
            }

            Boolean x = false;
            int k=0;
            //Label2.Text = EventHallBookingGridView.SelectedRow.Cells[4].Text;
            foreach (string id in eventhallid)
            {
                if (EventHallBookingGridView.SelectedRow.Cells[1].Text == id) //only HallID is same then compare.
                {
                    
                    //Label3.Text += "<br>"+ eventstartdate[k].ToString();
                    DateTime StartDate = Convert.ToDateTime(EventHallBookingGridView.SelectedRow.Cells[4].Text);
                    DateTime EndDate = Convert.ToDateTime(EventHallBookingGridView.SelectedRow.Cells[5].Text);
                    DateTime CompStartDate = Convert.ToDateTime(eventstartdate[k].ToString());
                    DateTime CompEndDate = Convert.ToDateTime(eventenddate[k].ToString());
                    if ((StartDate.Date <= CompStartDate.Date && EndDate.Date >= CompStartDate.Date) ||
                        (StartDate.Date <= CompEndDate.Date && EndDate.Date >= CompEndDate.Date) ||
                        (StartDate.Date >= CompEndDate.Date && EndDate.Date <= CompEndDate.Date))
                    {
                        Label1.Text = "EventHall booking time is conflict";
                        x = true;
                        break;
                    }
                    k++;
                }
            }

            if (x == false)
            {
                EventHallBookingGridView.SelectedRow.Cells[8].Text = "Approved";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < EventHallBookingGridView.Rows.Count; i++)
            {
                if (EventHallBookingGridView.Rows[i].Cells[8].Text == "Approved")
                {
                    int bookingId = Convert.ToInt32(EventHallBookingGridView.Rows[i].Cells[0].Text);
                    Label1.Text = bookingId.ToString();
                    IEventHallBooking ehb = af.getEHBData(bookingId).First();
                    ehb.Status = "Approved";
                    af.getEHBApproval();
                }
                else if (EventHallBookingGridView.Rows[i].Cells[8].Text == "Rejected")
                {
                    int bookingId = Convert.ToInt32(EventHallBookingGridView.Rows[i].Cells[0].Text);
                    Label1.Text = bookingId.ToString();
                    IEventHallBooking ehb = af.getEHBData(bookingId).First();
                    ehb.Status = "Rejected";
                    af.getEHBApproval();
                }
                Label1.Text = "Update Successful";
            }
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            GridViewRow row = (GridViewRow)((Button)sender).Parent.Parent;
            EventHallBookingGridView.SelectedIndex = row.RowIndex;
            EventHallBookingGridView.SelectedRow.Cells[8].Text = "Rejected";
            Label1.Text = null;
        }
    }
}